import type { Request,Response  } from "express";// 引入express 内部的类型定义

import db from "../../db/index"

// 定义controller 函数类型
const indexController : {
    [key in string]: (req: Request, res: Response) => void;
} = {}

// 获取所有商品
indexController.getGoods = (req, res) => {
    db.query('SELECT * FROM goods', (err: any, response: Array<Object>) => {
        if(err) throw err
        // res.json(users)
        console.log("######################## 调用 /getGoods ########################")

        res.send({
            code: 1,
            msg: "查询成功",
            data: response
        })
    })
    
};

// 获取商品详情
indexController.getGoodsDetails = (req, res) => {
    let id = req.body.id || null
    if(id && id == 1){
        db.query(`SELECT * FROM goods WHERE id = ${id}`, (err: any, response: Array<Object>) => {
            if(err) throw err
            // res.json(users)
            console.log("######################## 调用 /getGoodsDetails ########################")
    
            res.send({
                code: 1,
                msg: "查询成功",
                data: response[0]
            })
        })

    } 
    if(id && id != 1){
        res.send({
            code: 1,
            msg: "未查询到指定商品",
            data: null
        });
    }
    if(!id){
        res.send({
            code: 0,
            msg: "入参不正确",
            data: null
        })
    }
}

// 添加商品
indexController.addGoods = (req, res) => {
    let good = req.body || null
    if(!good){
        res.send({
            code: 0,
            msg: "入参不正确",
            data: null
        }) 
    }

    if(!good.name || good.name == ""){
        res.send({
            code: 0,
            msg: "输入的商品名称有误",
            data: null
        }) 
    }
    if(!good.price || good.price == ""){
        res.send({
            code: 0,
            msg: "输入的商品价格有误",
            data: null
        }) 
    }
    if(!good.date || good.date == ""){
        res.send({
            code: 0,
            msg: "输入的商品生产日期有误",
            data: null
        }) 
    }
    if(!good.image || good.image == ""){
        res.send({
            code: 0,
            msg: "上传的商品图片有误",
            data: null
        }) 
    }

    db.query(`INSERT INTO goods VALUES(${null},${good.name},${good.price},'元',${good.date},${good.image}`, 
        (err: any, response) => {
        if(err) throw err
        // res.json(users)
        console.log("######################## 调用 /addGoods ########################")

        res.send({
            code: 1,
            msg: "添加成功",
            data: response
        })
    })

}

// 删除商品
indexController.delGoods = (req, res) => {
    let id = req.body.id || null
    if(!id || id == ""){
        res.send({
            code: 0,
            msg: "删除失败，当前商品id不存在",
        })
    }

    db.query(`DELETE FROM goods WHERE id = ${id}`, 
        (err: any, response) => {
        if(err) throw err
        // res.json(users)
        console.log("######################## 调用 /delGoods ########################")

        res.send({
            code: 1,
            msg: "删除成功",
            data: response
        })
    })

}

// 修改商品
indexController.updateGoods = (req, res) => {
    let good = req.body || null
    if(!good || !good.id || good.id == ""){
        res.send({
            code: 0,
            msg: "入参不正确",
            data: null
        }) 
    }

    if(!good.name || good.name == ""){
        res.send({
            code: 0,
            msg: "商品名称不能为空",
            data: null
        }) 
    }
    if(!good.price || good.price == ""){
        res.send({
            code: 0,
            msg: "商品价格不能为空",
            data: null
        }) 
    }
    if(!good.date || good.date == ""){
        res.send({
            code: 0,
            msg: "商品生产日期不能为空",
            data: null
        }) 
    }
    if(!good.image || good.image == ""){
        res.send({
            code: 0,
            msg: "商品图片不能为空",
            data: null
        }) 
    }
    let setItems = ''
    for(let key in good){
        setItems += `${key} = ${good[key]},`
    }
    setItems = setItems.slice(0, -1);
    
    db.query(`UPDATE goods SET ${setItems} WHERE id = ${good.id}`, 
    (err: any, response) => {
        if(err) throw err
        // res.json(users)
        console.log("######################## 调用 /delGoods ########################")

        res.send({
            code: 1,
            msg: "修改成功",
            data: response
        })
    })

}



export default indexController